package com.abc.robot01.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.abc.robot01.R
import com.abc.robot01.activity.ShangPinKuActivity.Companion
import com.abc.robot01.retrofit.bean.Commodity
import com.abc.robot01.vivemodel.XiangQingViewModel
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage

/**
 * 商品详情
 */
class XiangQingActivity : BaseVoiceActivity() {

    private val viewModel: XiangQingViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val id = intent.getStringExtra("id")!!
        viewModel.loadProductDetail(id);
        setContent {
            val commodity by viewModel.commodity.collectAsState()

            val scrollState = rememberScrollState()
            MaterialTheme {
                Box {
                    Image(painter = painterResource(id = R.drawable.z1), contentDescription = "")
                    Row(
                        modifier = Modifier.padding(
                            start = 30.px,
                            end = 30.px,
                            top = 30.px
                        )
                    ) {
                        BackButton {
                        }
                        Spacer(modifier = Modifier.width(82.px))

                        Column(
                            modifier = Modifier
                                .fillMaxHeight()
                                .width(1400.px)
                        ) {
                            BasicText(
                                text = "详情",
                                style = TextStyle(
                                    fontSize = 30.mysp,
                                    color = Color.Black,
                                    fontWeight = FontWeight.Bold
                                ),
                            )
                            Spacer(modifier = Modifier.height(29.px))
                            MyCard {
                                Column(
                                    Modifier
                                        .padding(
                                            start = 30.px,
                                            end = 30.px,
                                            top = 30.px,
                                            bottom = 0.px
                                        )
                                        .verticalScroll(scrollState)
                                        .fillMaxSize(),
                                    verticalArrangement = Arrangement.spacedBy(30.px)
                                ) {
                                    Content(commodity)
                                }
                            }
                        }
                    }
                }
            }
        }
        Log.e(TAG, "onCreate");
    }

    @OptIn(ExperimentalGlideComposeApi::class)
    @Composable
    fun ScrollableImage(url: State<String?>) {
        // Remember scroll states for both horizontal and vertical scrolling
        val horizontalScrollState = rememberScrollState()
        val verticalScrollState = rememberScrollState()

        Box(
            modifier = Modifier
                .size(540.px) // Set the viewport size to 200x200
                .background(Color.LightGray) // Optional background for the frame
                .horizontalScroll(horizontalScrollState) // Enable horizontal scrolling
                .verticalScroll(verticalScrollState) // Enable vertical scrolling
        ) {
            GlideImage(
                model = url.value,
                contentDescription = "",
                modifier = Modifier
                    .clickable {
                    },
            )


            // Load and display the image with no scaling
//            Image(
//                painter = painterResource(id = R.drawable.x3),
//                contentDescription = "Scrollable Image",
////                modifier = Modifier.size(1000.dp), // The actual size of the image
//                contentScale = ContentScale.None // No scaling, show the image at its original size
//            )


        }
    }

    @OptIn(ExperimentalGlideComposeApi::class)
    @Composable
    fun ColumnScope.Content(commodity: Commodity?) {
        val images = viewModel.images.collectAsState().value?.split(",")
        val big = viewModel.bigImg.collectAsState()
        val detailImg by viewModel.detailImg.collectAsState()
        Row(horizontalArrangement = Arrangement.spacedBy(20.px)) {
            Column(verticalArrangement = Arrangement.spacedBy(20.px)) {
                images?.forEach { image ->
                    Log.e(TAG, "图片: $image")
                    GlideImage(
                        model = image,
                        contentDescription = "",
                        modifier = Modifier
                            .align(Alignment.CenterHorizontally)
                            .size(316.px, 176.px)
                            .clickable {
                                viewModel.bigImg.value = image
                            },
                    )

                }
            }
            ScrollableImage(big)
            Title()
        }
        Spacer(modifier = Modifier.height(40.px))
        //图片

        GlideImage(
            model = viewModel.detailImg.collectAsState().value?:R.drawable.detail,
            contentDescription = "",
            modifier = Modifier
                .align(Alignment.CenterHorizontally)
        )
    }

    @Composable
    fun RowScope.Title() {
        val commodity by viewModel.commodity.collectAsState()
        Column {
            Text(
                maxLines = 2,
                text = commodity?.name ?: "",
                style = TextStyle(
                    color = MYBLACK, fontSize = 30.mysp,
                )
            )

            Text(
                text = buildAnnotatedString {
                    withStyle(style = SpanStyle(fontSize = 24.mysp, color = MYRED)) {
                        append("¥")
                    }
                    withStyle(
                        style = SpanStyle(
                            fontSize = 42.mysp,
                            color = MYRED,
                            fontWeight = FontWeight.Bold
                        )
                    ) {
                        append(commodity?.price.toString() ?: "")
                    }
                    withStyle(style = SpanStyle(fontSize = 24.mysp, color = MYRED)) {
                        append("元/m²")
                    }
                }
            )

            Spacer(modifier = Modifier.height(15.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "风       格",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))

                if (commodity != null) {
                    val style = commodity!!.style
                    Log.e(TAG,"风格:"+style)
                    style?.let {
                    it.split(",").forEach { code ->
                        Text(
                            style = TextStyle(
                                color = MYBLACK, fontSize = 24.mysp,
                            ),
                            text = viewModel.question4List[code.toInt()-1].name,
                            modifier = Modifier
                                .background(
                                    color = MYBLACK_Percent5,
                                    shape = RoundedCornerShape(6.px)
                                ) // Set gray background
                                .padding(
                                    horizontal = 17.px,
                                    vertical = 10.px
                                ), // Set horizontal padding
                        )
                        Spacer(modifier = Modifier.width(10.px))
                    }
                        }
                }


            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "适用喜好",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                commodity?.interestName?.let {
                    Text(
                        style = TextStyle(
                            color = MYBLACK, fontSize = 24.mysp,
                        ),
                        text = commodity?.interestName ?: "",
                        modifier = Modifier
                            .background(
                                color = MYBLACK_Percent5,
                                shape = RoundedCornerShape(6.px)
                            ) // Set gray background
                            .padding(
                                horizontal = 17.px,
                                vertical = 10.px
                            ), // Set horizontal padding
                    )
                }
            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "色       系",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                commodity?.sxTypeName?.let {
                    Text(
                        style = TextStyle(
                            color = MYBLACK, fontSize = 24.mysp,
                        ),
                        text = it,
                        modifier = Modifier
                            .background(
                                color = MYBLACK_Percent5,
                                shape = RoundedCornerShape(6.px)
                            ) // Set gray background
                            .padding(
                                horizontal = 17.px,
                                vertical = 10.px
                            ), // Set horizontal padding
                    )
                }

            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "标       签",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )

                Spacer(modifier = Modifier.width(20.px))

                if (commodity != null) {
                    val labelNames = commodity!!.labelNames
                    labelNames?.split(",")?.forEach { name ->
                        Text(
                            style = TextStyle(
                                color = MYBLACK, fontSize = 24.mysp,
                            ),
                            text = name,
                            modifier = Modifier
                                .background(
                                    color = MYBLACK_Percent5,
                                    shape = RoundedCornerShape(6.px)
                                ) // Set gray background
                                .padding(
                                    horizontal = 17.px,
                                    vertical = 10.px
                                ), // Set horizontal padding
                        )
                        Spacer(modifier = Modifier.width(10.px))
                    }
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG, "onDestroy")
    }

    companion object {
        val TAG = "商品详情"
    }
}
